/*
 *   Copyright (c) 2010 Sonrisa Informatikai Kft. All Rights Reserved.
 * 
 *  This software is the confidential and proprietary information of
 *  Sonrisa Informatikai Kft. ("Confidential Information").
 *  You shall not disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into
 *  with Sonrisa.
 * 
 *  SONRISA MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 *  THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 *  TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 *  PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SONRISA SHALL NOT BE LIABLE FOR
 *  ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 *  DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

package com.zh.jbet.web.model;

import com.zh.jbet.core.entity.BaseEntity;
import com.zh.jbet.core.service.EntityService;
import com.zh.jbet.web.JBetServiceProvider;
import java.util.Iterator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.IFilterStateLocator;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.model.IModel;

/**
 *
 * @author sonrisa
 */
public class EntityDataProvider<T extends BaseEntity> extends SortableDataProvider<T> implements IFilterStateLocator{
    private JBetServiceProvider provider;
    private T filter;

    public EntityDataProvider(T filter, JBetServiceProvider provider) {
        this.provider = provider;
        this.filter = filter;
    }

    public EntityDataProvider(JBetServiceProvider provider) {
        this.provider = provider;
        this.filter = getService().getDefaultInstance();
    }

    @Override
    public T getFilterState() {
        return filter;
    }

    @Override
    public void setFilterState(Object state) {
        filter = (T)state;
    }

    @Override
    public Iterator<? extends T> iterator(int first, int count) {
        String sort = getSort() != null ? getSort().getProperty() : null;
        return getService().findByFilter(filter, sort, first, count).iterator();
    }

    @Override
    public IModel<T> model(T object) {
        return new EntityModel<T>(object, provider);
    }

    @Override
    public int size() {
        return getService().count(filter);
    }

    protected EntityService<T> getService() {
        return provider.getService();
    }
}
